create procedure wp_LoggerUpload
	@DBID int,
	@SchemaName varchar(max),
	@ObjectName varchar(max),
	@ObjectType varchar(max),
	@DBUser varchar(128),
	@Data varchar(max),
	@PostDate datetime,
	@Branch int = null,
	@Comment varchar(max) = null
as
	set nocount on

	declare @ObjectID int
	declare @State int
	declare @LastActivity datetime
	declare @LastInfoState int
	declare @LastDBID int

	if (not exists(select 1 from Objects where DBID = @DBID and SchemaName = @SchemaName and ObjectName = @ObjectName and ObjectType = @ObjectType))
		insert into Objects(
			DBID,
			SchemaName,
			ObjectName,
			ObjectType)
		select
			@DBID,
			@SchemaName,
			@ObjectName,
			@ObjectType

	select @ObjectID=ID
		from Objects
		where DBID = @DBID
			and SchemaName = @SchemaName
			and ObjectName = @ObjectName
			and ObjectType = @ObjectType

	insert into Data(
		ObjectID,
		Data)
	select
		@ObjectID,
		@Data

	select @State=@@identity

	select @LastActivity = max(PostDate) from Info i
		inner join Data d on i.State = d.State
		inner join Objects o on d.ObjectID = o.ID
		where DBID = @DBID

	select @LastInfoState = max(State) from Info
	select @LastDBID = DBID from Objects o
		inner join Data d on d.ObjectID = o.ID
		where d.State = @LastInfoState

	if (datediff(s, isnull(@LastActivity, '01/01/1990'), @PostDate) > 60 or @LastDBID <> @DBID)
	begin
		insert into Info(
			State,
			DBUser,
			PostDate,
			Comment,
			BranchID)
		select
			@State,
			@DBUser,
			@PostDate,
			@Comment,
			@Branch
	end

	set nocount off